/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Stanley
 */
public class DeThiDAO {

    public ResultSet getDeThi(int maLDT) {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();

            return query.executeQuery("select maDT, count(cauhoi) as SoCH from dethi left join ct_dethi on maDT = dethi "
                    + " where loaidethi = " + maLDT + " group by maDT order by maDT asc");
        } catch (Exception e) {
            return null;
        }
    }

    public ResultSet getLoaiDeThiByMaDT(String maDT) {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            String sql = "select * from dethi left join loaidethi on loaidethi = maLDT where maDT = " + maDT;
            return query.executeQuery(sql);
        } catch (Exception e) {
            return null;
        }
    }

    public ResultSet getChiTietDeThi(String maDT) {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            String sql = "select * from ct_dethi join cauhoi on cauhoi = maCH where dethi = " + maDT + " order by thutu asc";
            return query.executeQuery(sql);
        } catch (Exception e) {
            return null;
        }
    }

    public int getDeThiLonNhat() {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            String sql = "select max(maDT) as max from dethi";
            ResultSet rs = query.executeQuery(sql);
            rs.next();
            return rs.getInt("max");
        } catch (Exception e) {
            return -1;
        }
    }

    public boolean insertDeThi(String maDT, String maLDT) {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            String sql = "insert into dethi (maDT, loaidethi) values ( " + maDT + " , " + maLDT + " )";
            int result = query.executeUpdate(sql);
            return result == 0 ? false : true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean themCauHoiVaoDeThi(String maDT, String maCH, int thutu) {
        //query.executeUpdate("insert into ct_dethi (dethi, cauhoi, thutu) values ( " + maDT + " , " + maCH + " , " + Integer.toString(thutu) + " )");
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            String sql = "insert into ct_dethi (dethi, cauhoi, thutu) values ( " + maDT + " , " + maCH + " , " + Integer.toString(thutu) + " )";
            int result = query.executeUpdate(sql);
            return result == 0 ? false : true;
        } catch (Exception e) {
            return false;
        }
    }
}
